﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using ZhiHeng.OneCard.WpfClient.Model;

namespace ZhiHeng.OneCard.WpfClient.Common
{
    public static class FormUtility
    {
        public static string AttributeValue(this XElement xElement, string attributeName)
        {
            var xAttribute = xElement.Attribute(attributeName);
            return xAttribute != null ? xAttribute.Value : string.Empty;
        }

        public static List<Model.Menu> GetMenus()
        {
            string xpath = "Config/Menu.xml"; 
            XElement root = XElement.Load(xpath);
            return GetMenus(root).OrderBy(m => m.Order).ToList();
        }

        private static List<Model.Menu> GetMenus(XElement root)
        {
            var list = new List<Menu>();
            foreach (var xElement in root.Elements("Menu"))
            {
                var newMenu = new Model.Menu();
               
                newMenu.Id = xElement.AttributeValue("id");
                newMenu.Name = xElement.AttributeValue("name");
                newMenu.Order = int.Parse(xElement.AttributeValue("order"));
                string color = xElement.AttributeValue("tileColor");
                newMenu.TileColor = color;
                newMenu.Icon = xElement.AttributeValue("icon");
                newMenu.Form = xElement.AttributeValue("form");
                newMenu.Width = xElement.AttributeValue("Width");
                newMenu.Height = xElement.AttributeValue("Height");
                newMenu.BgColor1 = xElement.AttributeValue("bgColor1");
                newMenu.BgColor2 = xElement.AttributeValue("bgColor2");
              
                newMenu.Menus = GetMenus(xElement).OrderBy(m=>m.Order).ToList();
                list.Add(newMenu);
            }
            return list;
        }
        
        /// <summary>
        /// 16x16, 32x32, 48x48, 64x64
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static Icon ConvertImageToIcon(Image image)
        {
            return MakeIcon(image, 16, true);
        }

        static Icon MakeIcon(Image img, int size, bool keepAspRatio)
        {
            Bitmap square = new Bitmap(size, size);
            Graphics g = Graphics.FromImage(square);
            int x, y, w, h;
            if (!keepAspRatio || img.Height == img.Width)
            {
                x = y = 0;
                w = h = size;
            }
            else
            {
                float r = (float) img.Width/(float) img.Height;
                if (r > 1)
                {
                    w = size;
                    h = (int) ((float) size/r);
                    x = 0;
                    y = (size - h)/2;
                }
                else
                {
                    w = (int) ((float) size*r);
                    h = size;
                    y = 0;
                    x = (size - w)/2;
                }
            }
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(img, x, y, w, h);
            g.Flush();
            return Icon.FromHandle(square.GetHicon());
        }
    }
}
